/**
 * @author ljl  
 * @date 2018年9月28日 下午3:12:23 
 * @version 1.0 
 */
package com.edu.spider.controller;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * @author ljl
 * @date 2018年9月28日下午3:12:23
 */
@Component
@Slf4j
public class TestComponent {
	
	@Autowired
	JavaMailSender mailSender;

	public void testMail(){		
		long startTimestamp = System.currentTimeMillis();
        log.info("Start send mail ... ");
          
//          SimpleMailMessage message = new SimpleMailMessage();
          try {
        	MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    		MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true,"UTF-8");
			message.setFrom("287167584@qq.com");
			message.setTo("loujielong@ttsales.cn");
	          message.setSubject("测试邮件主题");
//	          message.setText("测试邮件内容");
	          message.setText("<html><head><meta http-equiv='Content-Type' content='text/html; charset=gbk'></head><body><h1>这是用实现HTML邮件发送的测试</body><img src=\'cid:image\'/></h1></html>", true);
	          ClassPathResource image = new ClassPathResource("/static/test2.jpg");
	          message.addInline("image", image);
	          //附件发送
//	          message.addAttachment("zhuxi.jpg", image);
	          this.mailSender.send(mimeMessage);
	          log.info("Send mail success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);
		} catch (MessagingException e) {
			log.error(e.getMessage(),e);
		}
	   
	}
	
}
